from PyQt5.QtWidgets import (QMessageBox, QFileDialog, QAbstractItemView)
from PyQt5.QtCore import Qt
import os

class ImageSelector:
    @staticmethod
    def select_images(parent=None):
        """
        选择图片文件或文件夹
        返回: (selected_files, is_folder)
              selected_files: 选择的文件列表或文件夹路径
              is_folder: 是否是文件夹选择
        """
        dialog = QMessageBox(parent)
        dialog.setWindowTitle("选择导入方式")
        dialog.setText("请选择导入方式:")
        dialog.setWindowFlags(dialog.windowFlags() | Qt.WindowStaysOnTopHint)
        
        btn_files = dialog.addButton("选择图片文件", QMessageBox.ActionRole)
        btn_folder = dialog.addButton("选择图片文件夹", QMessageBox.ActionRole)
        btn_cancel = dialog.addButton(QMessageBox.Cancel)
        
        dialog.exec_()
        
        if dialog.clickedButton() == btn_files:
            files = ImageSelector._select_image_files(parent)
            return (files, False) if files else (None, False)
        elif dialog.clickedButton() == btn_folder:
            folder = ImageSelector._select_image_folder(parent)
            return ([folder], True) if folder else (None, False)
        return None, False
    
    @staticmethod
    def _select_image_files(parent=None):
        file_dialog = QFileDialog(parent)
        file_dialog.setFileMode(QFileDialog.ExistingFiles)
        file_dialog.setNameFilter("图片文件 (*.png *.jpg *.jpeg *.bmp *.gif)")
        
        if file_dialog.exec_() == QFileDialog.Accepted:
            return file_dialog.selectedFiles()
        return None
    
    @staticmethod
    def _select_image_folder(parent=None):
        folder = QFileDialog.getExistingDirectory(
            parent, 
            "选择图片文件夹",
            options=QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks
        )
        return folder if folder else None